import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import * as dto from './DTO';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/parts/api/v1';

const getInitList = formatMessage({
    id: 'api.getInitList',
    defaultMessage: '查询初始数据'
});

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询信用证数据'
});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询信用证数据详情'
});

const getRegisterDesc = formatMessage({
    id: 'api.getRegister',
    defaultMessage: '登记信用证数据信息'
});

const getReceiveDesc = formatMessage({
    id: 'api.getReceive',
    defaultMessage: '到款登记数据详情信息'
});

const getAreasDesc = formatMessage({
    id: 'api.message.getAreasDesc',
    defaultMessage: '查询国际区域'
});

const getDealerDesc = formatMessage({
    id: 'api.message.getDealerDesc',
    defaultMessage: '查询总代编号'
});

const getDealerSimpleDesc = formatMessage({
    id: 'api.message.getDealerSimpleDesc',
    defaultMessage: '快捷查询总代编号'
});

const getMarketDepartmentsDesc = formatMessage({
    id: 'api.message.getMarketDepartmentsDesc',
    defaultMessage: '查询国际大区信息'
});


const exportDesc = formatMessage({
    id: 'api.exportData',
    defaultMessage: '导出信用证信息'
});

const getCloseDesc = formatMessage({
    id: 'api.getClose',
    defaultMessage: '关闭信用证信息'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/lcCreditManagement/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitList));

// 查询信用证数据列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/lcCreditManagements?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询信用证数据详情
export const getDetail = id => request(`${baseUrl}/lcCreditManagements/${id}`, null, dto.Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 新增
export const newOrder = data => request(`${baseUrl}/lcCreditManagements`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getRegisterDesc));

// 到款登记修改
export const receiveData = (id, data) => request(`${baseUrl}/lcCreditManagements/${id}/receive`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getReceiveDesc));

// 查询国际区域列表
export const getRegions = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/areaTileds/forSubDealer?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};

// 查询经销商
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};

//根据条件查询总代子公司
export const getDealersQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forPartSaleOrder/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerSimpleDesc));
};

// 根据国际区域查询大区
export const getMarketDepartments = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/marketDepartments/forSubDealer?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentsDesc));
};
//导出
export const exportLccCreditManagement = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/lcCreditManagements/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc));
};

// 关闭
export const closeOrder = (id, rowVersion) => request(`${baseUrl}/lcCreditManagements/${id}/close`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        rowVersion
    })
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getCloseDesc));
